package com.dankegongyu.demo.http;

import android.app.Application;

import com.dankegongyu.demo.http.bean.CityBean;
import com.dankegongyu.demo.http.bean.ConfigBean;
import com.dankegongyu.demo.http.bean.DeviceRegisterReq;
import com.dankegongyu.demo.http.bean.DeviceRegisterRsp;
import com.dankegongyu.demo.http.bean.FavoriteRoomReq;
import com.dankegongyu.demo.http.bean.FavoriteRoomRsp;
import com.dankegongyu.demo.http.bean.OauthAccessTokenBody;
import com.dankegongyu.demo.http.bean.TokenBean;
import com.dankegongyu.demo.http.bean.UserBean;
import com.dankegongyu.demo.http.http.DKObserver;
import com.dankegongyu.demo.http.http.HttpError;
import com.dankegongyu.demo.http.http.api.Api;
import com.dankegongyu.demo.http.http.interceptor.CacheInterceptor;
import com.dankegongyu.demo.http.http.interceptor.DeviceRegisterInterceptor;
import com.dankegongyu.demo.http.http.interceptor.ParamInterceptor;
import com.dankegongyu.demo.http.http.interceptor.ParseInterceptor;
import com.dankegongyu.demo.http.util.DKHttpUtil;
import com.dankegongyu.lib.http.DKHttp;
import com.dankegongyu.lib.http.exception.ApiException;
import com.dankegongyu.lib.http.observer.DKHttpBaseObserver;
import com.dankegongyu.lib.http.utils.DKHttpLogUtils;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2018/6/12 16:12
 */
@SuppressWarnings("ResultOfMethodCallIgnored")
public class MyApplication extends Application {

    public static final String GET = "GET -> ";
    public static final String POST = "POST -> ";
    public static final String PUT = "PUT -> ";
    public static final String DELETE = "DELETE -> ";
    private static MyApplication sInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;

        init();

//        get();
//        post();
//        put();
//        delete();
        flatMap();
//        gankio();

        testNull();
    }

    public static MyApplication getInstance() {
        return sInstance;
    }

    private void init() {
        DKHttp.setDebugMode(true);
        DKHttp.getInstance()
                .setTimeout(10_000)
                .addInterceptor(new CacheInterceptor()) // 离线缓存拦截器
                // 缓存路径：SDCard/Android/data/包名/cache/ 缓存大小：10M
                .setCache(new File(getExternalCacheDir(), "http"), 10 * 1024 * 1024)
                .addInterceptor(new ParamInterceptor())
                .addInterceptor(new ParseInterceptor());
    }

    private void get() {
        // 返回原始json数据
        DKHttp.get(Api.BASE_CONFIG)
                .param("city_id", "1")
                .execute()
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        DKHttpLogUtils.e(GET + s);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        // 返回对象
        DKHttp.get(Api.BASE_CONFIG)
                .param("city_id", "1")
                .execute(ConfigBean.class)
                .subscribeOn(Schedulers.io())
                .subscribe(new DKObserver<ConfigBean>() {
                    @Override
                    public void onNext(ConfigBean configBean) {
                        DKHttpLogUtils.e(GET + configBean);
                    }
                });

        // 返回数组
        DKHttp.get(Api.CITY_LIST)
                .addHeader("custom", "wuha")
                .execute(new TypeToken<List<CityBean>>() {
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CityBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<CityBean> list) {
                        DKHttpLogUtils.e(GET + list);
                    }

                    @Override
                    public void onError(Throwable e) {
//                        DKHttpLogUtils.e(GET + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

        // newBuilder
        DKHttp.get(Api.CITY_LIST, DKHttpUtil.getDKHttpBuilderV3())
                .addHeader("custom", "wuha")
                .execute(new TypeToken<List<CityBean>>() {
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<CityBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<CityBean> list) {
                        DKHttpLogUtils.e(GET + list);
                    }

                    @Override
                    public void onError(Throwable e) {
//                        DKHttpLogUtils.e(GET + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void post() {
        DKHttp.post(Api.DEVICE_REGISTER)
                .body(new DeviceRegisterReq())
                .addInterceptor(new DeviceRegisterInterceptor())
                .execute(DeviceRegisterRsp.class)
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<DeviceRegisterRsp>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DeviceRegisterRsp deviceRegisterRsp) {
                        DKHttpLogUtils.e(POST + deviceRegisterRsp.toString());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void put() {
        DKHttp.put(Api.FAVORITE_ROOM)
                .setTimeout(20_000)
                .body(new FavoriteRoomReq("1875513774"))
                .execute(FavoriteRoomRsp.class)
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<FavoriteRoomRsp>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FavoriteRoomRsp favoriteRoomRsp) {
                        DKHttpLogUtils.e(PUT + favoriteRoomRsp.toString());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void delete() {

    }

    private void flatMap() {
        DKHttp.post(Api.OAUTH_LOGIN)
                .body(new OauthAccessTokenBody("13000049962", "1111"))
                .execute(TokenBean.class)
                .subscribeOn(Schedulers.io())
                .doOnNext(new Consumer<TokenBean>() {
                    @Override
                    public void accept(TokenBean tokenBean) throws Exception {
                        DKHttpLogUtils.e(POST + tokenBean);
                    }
                })
                .flatMap(new Function<TokenBean, Observable<UserBean>>() {
                    @Override
                    public Observable<UserBean> apply(@NonNull TokenBean tokenBean) throws Exception {
                        // 登录成功拿到token接着调用获取用户信息接口
                        return DKHttp.get(Api.USER_INFO).execute(UserBean.class);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DKObserver<UserBean>() {
                    @Override
                    public void onNext(@NonNull UserBean userBean) {
                        DKHttpLogUtils.e(GET + userBean);
                    }

                    @Override
                    protected void onError(@NonNull HttpError httpError) {
                        super.onError(httpError);

                    }
                });
    }

    private void gankio() {
        DKHttp.get("http://gank.io/api/xiandu/categories")
                .execute()
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        DKHttpLogUtils.e(GET + "Welcome to gank.io ~");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void testNull() {
        DKHttp.get(Api.ROOM_LIST)
                .param("city_id", "1")
                .param("page", "1")
                .param("isnewformat", 1)
                .param("search_text", null) // 测试null
                .execute()
                .subscribeOn(Schedulers.io())
                .subscribe(new DKHttpBaseObserver<String>() {
                    @Override
                    protected void onSuccess(String data) {

                    }

                    @Override
                    protected void onFailure(ApiException e) {

                    }
                });
//                .subscribe(new Observer<String>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(String s) {
//                        DKHttpLogUtils.e(GET + "房源列表" + s);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        DKHttpLogUtils.e(GET + "房源列表" + e);
//                    }
//
//                    @Override
//                    public void onComplete() {
//
//                    }
//                });
    }
}
